<?php
namespace Hive\Ressource\Document;
/**
 * Document Parser.
 *
 * The parser is use to extract some information
 * from the document to initialize contact creation
 *
 * @author Xavier VILAIN
 * @package Hive\Ressource\Document
 *

 */
use Hive\Ressource;

class Parser {

	public static function parse($content = null) {
		$fields = array();
		// Email1
		if(preg_match_all('#([-a-z0-9._]+@[-a-z0-9]+[.]+[-a-z0-9]{2,4}+)#', $content, $matches)) {
			/*
			foreach($matches[0] as $match) {
				$match = trim($match,'_.-@');
				$distinctMatches[$match] = $match;
			}
			$maxLength = 0;
			foreach($distinctMatches as $match) {
				$length = strlen($match);
				if($length >= $maxLength) {
					$fields['email'] = $match;
					$maxLength = $length;
				}
			}
			*/
			$fields['email'] = $matches[0][0];
			$parts = preg_split("/[@]/",$fields['email']);
			if(preg_match("/[._]/",$parts[0],$namings)) {
				$fields['last_name'] = null;
				$nameParts = preg_split("/[._]/",$parts[0]);
				if(count($nameParts) == 2) {
					$fields['first_name'] = $nameParts[0];
					for($i=1;$i<count($nameParts);$i++) {
						$fields['last_name'] .= strtoupper($nameParts[$i]);
					}
				} else {
					$fields['first_name'] = "Not Founded";
					$fields['last_name'] = preg_replace("/[._]/"," ",strtoupper($parts[0]));
				}
				
			} else {
				$fields['first_name'] = "Not Founded";
				$fields['last_name'] = preg_replace('/[0-9]/','',strtoupper($parts[0]));
			}
			// Removing Numbers
			if(!empty($fields['last_name'])) {
				$fields['last_name'] = preg_replace('/[0-9]/','',$fields['last_name']);
				$fields['first_name'] = preg_replace('/[0-9]/','',$fields['first_name']);
			}
		}
		// Phone1
		if(preg_match_all("/06+([: .0-9-]{12})/", $content, $matches)) {
			$fields['phone1'] = preg_replace("/[: .-]/"," ",$matches[0][0]);
		} elseif(preg_match_all("/6+([: .0-9-]{12})/", $content, $matches)) {
			$firstMatch = '0'.$matches[0][0];
			$fields['phone1'] = preg_replace("/[: .-]/"," ",$firstMatch);
		} elseif(preg_match_all("/336+([: .0-9-+]{12})/", $content, $matches)) {
			$firstMatch = $matches[0][0];
			$firstMatch = preg_replace("/336/","06",$firstMatch);
			$fields['phone1'] = preg_replace("/[: .-]/"," ",$firstMatch);
		} elseif(preg_match_all("/\+34+([: .0-9-]{12})/", $content, $matches)) {
			$firstMatch = $matches[0][0];
			$fields['phone1'] = preg_replace("/[: .-]/"," ",$firstMatch);
		}
		if(!empty($fields['phone1'])) {
			$fields['phone1'] = str_replace("+"," ",$fields['phone1']);
		}
		
		// Birth Date
		$pos = strpos($content,'nee le');
		if($pos !== false) {
			// find first date from the position of "born on" 
			$subcontent = substr($content,$pos);
			if(preg_match('#([0-9]{1,2}[ .-]{1}[0-9]{1,2}[ .-]{1}[0-9]{2,4})#',$subcontent,$matches)) {
				foreach($matches as $match) {
					try {
						$parts = preg_split('#([ .-])#',$match);
						if(count($parts) != 3) {
							continue;
						}
						if($parts['2'] < 100) {
							$year = '20'.$parts['2'];
						} else {
							$year = $parts['2'];
						}
						$datestamp = $year.'-'.$parts['1'].'-'.$parts['0'];
						$dateTime = new \DateTime($datestamp);
						$distinctMatches[] = $datestamp;
					} catch(\Exception $e) {
						continue;
					}
				}
				if(isset($distinctMatches)) {
					asort($distinctMatches);
					$dateTime = new \DateTime($distinctMatches[0]);
					$fields['birth_date'] = $dateTime->format('Y-m-d');
				}
				
			} 
		}
		
		return $fields;
	}
}